import 'dart:developer';

import 'package:flutter/material.dart';

class BlockRefreshWithInheritedWidget extends StatefulWidget {
  const BlockRefreshWithInheritedWidget({Key? key}) : super(key: key);

  @override
  _BlockRefreshWithInheritedWidgetState createState() =>
      _BlockRefreshWithInheritedWidgetState();
}

class _BlockRefreshWithInheritedWidgetState
    extends State<BlockRefreshWithInheritedWidget> {
  int currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return PageManageProvider(
        currentIndex: currentIndex,
        onTabChanged: (index) {
          setState(() {
            currentIndex = index;
          });
        },
        child: Scaffold(
          appBar: AppBar(
            title: Text("InheritedWidget局部刷新"),
            centerTitle: true,
          ),
          body: Container(
            child: Row(
              children: [
                Container(
                  color: Colors.grey[100],
                  width: 100,
                  child: LeftMenuWidget(),
                ),
                Expanded(child: RightMenu())
              ],
            ),
          ),
        ));
  }
}

class PageManageProvider extends InheritedWidget {
  final int currentIndex;
  final ValueChanged onTabChanged;
  final Widget child;

  PageManageProvider(
      {required this.currentIndex,
      required this.onTabChanged,
      required this.child})
      : super(child: child);

  static PageManageProvider of(BuildContext context) {
    final PageManageProvider? provider =
        context.dependOnInheritedWidgetOfExactType<PageManageProvider>();
    return provider!;
  }

  @override
  bool updateShouldNotify(covariant PageManageProvider oldWidget) {
    return currentIndex != oldWidget.currentIndex;
  }
}

class LeftMenuWidget extends StatefulWidget {
  const LeftMenuWidget({Key? key}) : super(key: key);

  @override
  _LeftMenuWidgetState createState() => _LeftMenuWidgetState();
}

class _LeftMenuWidgetState extends State<LeftMenuWidget> {
  List<String> list = ["菜单1", "菜单2", "菜单3"];

  @override
  Widget build(BuildContext context) {
    var provider = PageManageProvider.of(context);
    return Container(
      child: ListView.separated(
          itemBuilder: (context, index) {
            return ListTile(
              title: Text(
                list[index],
                style: Theme.of(context).textTheme.bodyText1,
              ),
              onTap: () {
                provider.onTabChanged(index);
              },
            );
          },
          separatorBuilder: (context, index) {
            return Divider();
          },
          itemCount: list.length),
    );
  }
}

class RightMenu extends StatefulWidget {
  const RightMenu({Key? key}) : super(key: key);

  @override
  _RightMenuState createState() => _RightMenuState();
}

class _RightMenuState extends State<RightMenu> {
  @override
  Widget build(BuildContext context) {
    var provider = PageManageProvider.of(context);
    return Container(
      child: Center(
        child: Text("${provider.currentIndex}"),
      ),
    );
  }
}
